package com.webserver.core;

import com.webserver.http.HttpRequest;
import com.webserver.http.HttpResponse;

import java.io.File;

public class DispatcherServlet {
    public void service(
        HttpRequest request,HttpResponse response){
        String path = request.getUrl();
        File file = new File("./webapps"+path);

        if(file.exists() && file.isFile()) {
            //资源存在
            response.setEntily(file);
            response.addHeader("Content-Type","text/html");
            response.addHeader("Content-Length",file.length()+"");
        }else {
            //资源不存在
            response.setStatusCode(404);
            response.setStatusReason("NotFound");
            file = new File("./webapps/root/404.html");
            response.setEntily(file);
            response.addHeader("Content-Type","text/html");
            response.addHeader("Content-Length",file.length()+"");
        }
        response.addHeader("Server","WebServer");
    }
}
